<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html-el" %>
<%@ taglib uri="http://amc.af.mil/radtaglibrary" prefix="rad" %>
<%@ taglib uri="http://amc.af.mil/portal-el" prefix="portal" %>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<html>
<head><title>Item Detail</title></head>

<body>

<rad:useBean id="equipmentItemViewHelper"/>
<rad:useBean id="maintenanceViewHelper"/>
<rad:useBean id="organizationViewHelper"/>

<c:set var="equipmentItem" value="${equipmentItemViewHelper.item}"/>

<div id="portal-left-bar">
    <jsp:include page="../navigation/navigationMenu.jsp"/>
</div>

<div id="portal-content">
<portal:box title="Equipment Information" style="width:800px">
<portal:subSection title="Equipment Details">
    <table>
        <tr>
            <td>Serial Number:</td>
            <td><c:out value="${equipmentItem.serial}"/></td>
        </tr>
        <tr>
            <td>Manufacturer:</td>
            <td><c:out value="${equipmentItem.itemModel.manufacturer.name}"/></td>
        </tr>
        <tr>
            <td>Model:</td>
            <td><c:out value="${equipmentItem.itemModel.name}"/></td>
        </tr>
        <c:choose>
            <c:when test="${equipmentItem.itemStatus.ordinal ne 3 && equipmentItem.itemStatus.ordinal ne 5}">
                <tr>
                    <td>Assigned To:</td>
                    <td>
                        <c:choose>
                            <c:when test="${equipmentItem.assignedUser eq null}">
                                (<c:out value="${equipmentItem.location}"/>)
                            </c:when>
                            <c:otherwise>
                                <c:out value="${equipmentItem.assignedUser.displayName}"/>
                            </c:otherwise>
                        </c:choose>

                    </td>
                </tr>
            </c:when>
        </c:choose>
        <tr>
            <td>Type:</td>
            <td><c:out value="${equipmentItem.itemModel.type.name}"/></td>
        </tr>
        <tr>
            <td>Purchase Date:</td>
            <td>
                <fmt:formatDate value="${equipmentItem.purchaseDate}" type="date"
                                dateStyle="medium"/>
            </td>
        </tr>
        <tr>
            <td>Original Cost:</td>
            <td><c:out value="${equipmentItemViewHelper.originalCost}"/></td>
        </tr>
        <tr>
            <td>Depreciated Value:</td>
            <td><c:out value="${equipmentItemViewHelper.depreciatedValue}"/></td>
        </tr>
    </table>

</portal:subSection>
<portal:subSection title="Assign To User">
    <c:choose>
        <c:when test="${equipmentItem.itemStatus.ordinal ne 3 && equipmentItem.itemStatus.ordinal ne 5}">
            <html:form method="post" action="assignEquipmentItemToUser.do">
                <html:hidden property="itemId" value="${equipmentItem.id}"/>
                <table style="width: 100%">
                    <tr>
                        <th>Organization</th>
                        <th>User</th>
                        <th>Actions</th>
                    </tr>
                    <tr style="text-align:center">
                        <td>
                            <html:select property="organizationId"
                                         styleId="organizationId"
                                         onchange="javascript:chainSelect('organizationId','userId','user/getAllByOrganization.do');">
                                <html:option value="-1">Please Select an Organization</html:option>
                                <html:optionsCollection name="organizationViewHelper"
                                                        property="all"
                                                        value="id"
                                                        label="name"/>
                            </html:select>
                        </td>
                        <td>
                            <html:select property="userId"
                                         styleId="userId">
                                <html:option value="-1">Please Select an Organization</html:option>
                            </html:select>
                        </td>
                        <td>
                            <html:submit value="Assign"/>
                        </td>

                    </tr>
                </table>
            </html:form>
        </c:when>
        <c:otherwise>
            <p>Item is no longer in Active inventory and cannot be reassigned.</p>
        </c:otherwise>
    </c:choose>
    <br/>
</portal:subSection>
<br/>
<portal:subSection title="Unassign From User">
    <p><html:form method="post" action="equipmentItem/unassignUserFromEquipment.do">
        <html:hidden property="page" value="viewItem"/>
        <html:hidden property="itemId" value="${equipmentItem.id}"/>
        <div style="text-align:center">
            <c:choose>
                <c:when test="${equipmentItem.assignedUser eq null}">
                    <p>Item Not Assigned</p>
                </c:when>
                <c:otherwise>
                    <html:submit value="Unassign"/>
                </c:otherwise>
            </c:choose>
        </div>
    </html:form></p>
    <br/>
</portal:subSection>
<portal:subSection title="Update Location">
    <c:choose>
        <c:when test="${equipmentItem.itemStatus.ordinal ne 3 && equipmentItem.itemStatus.ordinal ne 5}">
            <p>Note: This will action will unassign equipment from users. It is only meant to note the location of
                unassigned equipment.</p>
            <br/>

            <p><html:form method="post" action="updateLocation.do">
                <html:hidden property="itemId" value="${equipmentItem.id}"/>
                <label for="location">Location:</label>
                <html:text property="location" size="20" maxlength="20"/>
                <html:submit value="Update Location"/>
            </html:form></p>
        </c:when>
        <c:otherwise>
            <p>This item is no longer in inventory and cannot have its location updated.</p>
        </c:otherwise>
    </c:choose>
    <br/>
</portal:subSection>
<portal:subSection title="Maintenance Log">
    <display:table name="maintenanceViewHelper.itemEntries" excludedParams="method" style="width: 100%">
        <display:column property="formattedDate" title="Entry Date" sortable="true"/>
        <display:column property="entryText" title="Text"/>
    </display:table>
    <html:link
            action="maintenanceEntry/viewAddEntry?itemId=${equipmentItem.id}">Create Entry</html:link>
</portal:subSection>
</portal:box>
</div>
</body>
</html>